package net.sansi.v3correctserver.service.camera.impl;

import lombok.extern.slf4j.Slf4j;
import net.sansi.v3correctserver.constenum.AppHelper;
import net.sansi.v3correctserver.entity.space.CorrectCnf;
import net.sansi.v3correctserver.service.camera.Camera;
import net.sansi.v3correctserver.service.camera.CameraService;
import net.sansi.v3correctserver.service.camera.CameraTcpService;
import net.sansi.v3correctserver.service.space.ProjectSpace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.file.Path;
import java.util.List;

/**
 * Created by Fang
 * create time : 2024/11/4
 * IDE : IntelliJ IDEA
 * <p>
 * 相机服务
 */

@Service
@Slf4j
public class CameraServiceImpl implements CameraService {

    @Resource
    private CameraTcpService cameraTcpService;

    /**
     * 拍照
     *
     * @param params   相机参数
     * @param savePath 输出路径
     */
    @Override
    public Path takePhoto(Camera.Params params, Path savePath) {
        takePhoto(params, savePath,
                AppHelper.projectSpace().getCorrectCnf().getCamera()
        );
        return null;
    }

    /**
     * 拍照
     *
     * @param params   拍摄参数
     * @param savePath 保存路径
     * @param Camera   相机
     * @return 保存路径
     */
    @Override
    public Path takePhoto(Camera.Params params, Path savePath, Camera Camera) {
        Camera.takePhoto(params, savePath);
        return savePath;
    }


    /**
     * 获取相机列表
     *
     * @return 相机列表
     */
    @Override
    public List<String> listCameraNames() {
        return Camera.cameras().stream().map(Camera::getName).toList();
    }

    /**
     * 连接相机
     *
     * @param cameraName 相机名称
     */
    @Override
    public void connectCamera(String cameraName) {
        ProjectSpace projectSpace = AppHelper.projectSpace();
        CorrectCnf correctCnf = projectSpace.getCorrectCnf();
        Camera camera = Camera.find(cameraName);
        camera.checkReady();
        correctCnf.setCameraName(cameraName);
        Camera.Options options = camera.readOptions(false);
        for (Camera.Params cameraParams : correctCnf.getCameraParams()) {
            cameraParams.resetByOptions(options);
        }
        projectSpace.saveCorrectCnf(correctCnf);
    }


}
